package com.jframe.sys.controller.utils;

import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import net.sf.json.JSONObject;

import com.jframe.app.model.DataSourse;
import com.jframe.app.service.DataSourseService;
import com.jframe.cfg.model.DicType;
import com.jframe.cfg.model.Dictionary;
import com.jframe.cfg.service.DictypeService;
import com.jframe.sys.orm.dao.impl.JdbcTemp;
import com.jframe.sys.orm.jdbc.JdbcProperties;
import com.jframe.sys.utils.CacheUtil;
import com.jframe.sys.utils.JsonUtil;
import com.jframe.sys.utils.SpringUtil;
import com.jframe.sys.utils.StringUtil;

public class DicCache {
	
	public static Object getObject(String key){
		CacheUtil cache = CacheUtil.getInstance("dictionarydata");
		return cache.get(key);
	}
	
	public static void putObject(String key,String json){
		CacheUtil cache = CacheUtil.getInstance("dictionarydata");
		cache.put(key, json);
	}
	
	/**
	 *  获取easyuicombobox的json
	 * @param key
	 * @return
	 */
	public static String getComboboxJson(String key){
		String json = (String)getObject(key);
		if(StringUtil.null2Space(json).equals("")||StringUtil.null2Space(json).equals("[]")){
			try{
				reload(key);
				json = (String)getObject(key);	
			}catch(Exception e){
				json="";
			}
		}
		return json;
	}
	
	/**
	 * 
	 * @param key 
	 * @param isReload 是否实时获取数据库字典数据
	 * @return
	 */
	public static String getComboboxJson(String key,boolean isReload){
		String json="";
		if(isReload){
			try{
				reload(key);
				json = (String)getObject(key);				
			}catch(Exception e){
				json="";
			}
		}else{
			json = getComboboxJson(key);
		}
		return json;
	}
	
	/**
	 *  获取easyuicombotree的json
	 * @param key
	 * @return
	 */
	public static String getCombotreeJson(String key){
		return getComboboxJson(key);
	}
	
	/**
	 * 
	 * @param key
	 * @param isReload 是否实时获取数据库字典数据
	 * @return
	 */
	public static String getCombotGridJson(String key,boolean isReload){
		if(isReload){
			return getComboboxJson(key,isReload);
		}else{
			return getComboboxJson(key);
		}
	}
	
	/**
	 * 获取easyuicombogrid的json
	 * @param key
	 * @return
	 */
	public static String getComboGridJson(String key){
		return getComboboxJson(key);
	}
	
	/**
	 * 
	 * @param key
	 * @param isReload 是否实时获取数据库字典数据
	 * @return
	 */
	public static String getCombotreeJson(String key,boolean isReload){
		if(isReload){
			return getComboboxJson(key,isReload);
		}else{
			return getComboboxJson(key);
		}
	}
	
	public static void reload(String key){
		CacheUtil cache = CacheUtil.getInstance("dictionarycache");
		DictypeService  dictypeService = (DictypeService)SpringUtil.getBean("dictypeService");
		DicType dictype = dictypeService.load("enname",key);
		JdbcTemp jdbcTemp = new JdbcTemp();
		String sql = "select * from t_sys_dictionary where typeId = "+dictype.getId();
		List<Dictionary> list = JsonUtil.json2List(JsonUtil.list2Json(jdbcTemp.queryForList(getConnName(dictype),sql)),Dictionary.class);
		String json = createComboboxJson(list);
		cache.put(dictype.getEnname(), json);
		cache.put(dictype.getEnname()+"_tree", json);
	}
	
	private static String createComboboxJson(List<Dictionary> list){
		String json = "[";
		for(Dictionary dictionary:list){
			json +="{";
			json +="\"id\":\""+dictionary.getCode()+"\",";
			json +="\"text\":\""+dictionary.getValue()+"\"";
			json +="},";
		}
		if(list.size()>0){
			json = json.substring(0,json.length()-1);
		}
		json +="]";
		return json;
	}
	
	private static String getConnName(DicType dictype){		
		DataSourseService dataSourseService = (DataSourseService)SpringUtil.getBean("dataSourseService");
		List<DataSourse> dataSoursels = dataSourseService.findBySql(DataSourse.class, "select * from t_app_datasourse where appId = 1");
		DataSourse dataSourse = dataSoursels.get(0);
		return dataSourse.getConName();		
	}
	
	/**
	 * 根据key值获取对应的value值
	 * @param key
	 * @param code
	 * @return
	 */
	public static String getCodeValue(String key,String code){
		String json = getComboboxJson(key);
		Map map = json2Map(json);
		return (String)map.get(code);
	}
	
	 private static Map json2Map(String jsonStr){
    	Map map = new HashMap();
    	String [] jsonArray = JsonUtil.json2StringArray(jsonStr);
    	for(String json:jsonArray){
    		JSONObject jsonObject = JSONObject.fromObject(json);  
            Iterator  keyIter = jsonObject.keys();
            String key;
            Object value;
            String putname = "";
            String putvalue = "";
            while( keyIter.hasNext())
            {
                key = (String)keyIter.next();
                value = jsonObject.get(key);
                if(key.equals("id")){
                	putname = (String) value;
                }else{
                	putvalue = (String) value;
                }                
            }
            map.put(putname, putvalue);
    	}
    	return map;
    }
	
}
